package com.my.app.tools.ui.awt;

import com.google.common.collect.Maps;
import com.my.app.tools.pojo.em.EditToolType;
import com.my.app.tools.util.AppToolUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Map;


public class ScreenShotToolsWindow extends JWindow {

    private ScreenShotMainWindow parent;

    private Map<EditToolType, JToggleButton> editToolButtons = Maps.newHashMap();

    private JButton colorButton = buildColorButton();
    private JComboBox weightComboBox = buildWeightComboBox();
    private JComboBox lineTypeComboBox = buildLineTypeComboBox();

    private JButton confirmButton = buildCloseButton("确定", false,true);
    private JButton closeButton = buildCloseButton("取消", true,false);
    private JButton copyButton = buildCloseButton("复制", true,true);

    public ScreenShotToolsWindow(ScreenShotMainWindow parent) {
        this.parent = parent;
        this.init();
        this.pack();
        this.setVisible(true);
    }

    private void init() {
        this.setLayout(new BorderLayout());
        JToolBar toolBar = new JToolBar();
        toolBar.add(colorButton);
        toolBar.add(weightComboBox);
        toolBar.add(lineTypeComboBox);
        toolBar.addSeparator();
        for (EditToolType editToolType : EditToolType.values()) {
            JToggleButton button = buildEditToolButton(editToolType);
            editToolButtons.put(editToolType, button);
            toolBar.add(button);
        }
        toolBar.addSeparator();
        toolBar.add(copyButton);
        toolBar.add(confirmButton);
        toolBar.add(closeButton);

        toolBar.setFloatable(false);
        this.add(toolBar, BorderLayout.NORTH);
    }

    private JComboBox buildWeightComboBox() {
        JComboBox comboBox = new JComboBox(new Object[]{1, 2, 3, 4, 5});
        comboBox.setSelectedIndex(1);
        comboBox.addItemListener(e -> {
            Object val = e.getItem();
            Integer weight = Integer.valueOf(String.valueOf(val));
            EditToolType.setWeight(weight);
        });
        return comboBox;
    }

    private JComboBox buildLineTypeComboBox() {
        JComboBox comboBox = new JComboBox(new Object[]{"实线", "虚线"});
        comboBox.setSelectedIndex(0);
        comboBox.addItemListener(e -> {
            EditToolType.setDottedLine(comboBox.getSelectedIndex() == 1);
        });
        return comboBox;
    }

    private JButton buildCloseButton(String text, boolean cleanImage, boolean copyImageToClipboard) {
        JButton button = new JButton(text);
        button.addActionListener(e -> {
            close(cleanImage, copyImageToClipboard);
        });
        return button;
    }

    private JToggleButton buildEditToolButton(EditToolType editToolType) {
        JToggleButton button = new JToggleButton(editToolType.getDesc());
        button.setSelected(editToolType == EditToolType.AGAIN);
        button.addActionListener(e -> {
            for (Map.Entry<EditToolType, JToggleButton> entry : editToolButtons.entrySet()) {
                entry.getValue().setSelected(entry.getKey() == editToolType);
            }
            parent.setEditToolType(editToolType);
        });
        return button;
    }

    private JButton buildColorButton() {
        JButton button = new JButton("颜色");
        button.setForeground(Color.RED);
        button.addActionListener(e -> {
            Color color = JColorChooser.showDialog(this, "选择颜色", button.getForeground());
            if (color == null) {
                return;
            }
            button.setForeground(color);
            EditToolType.setColor(color);
        });
        return button;
    }

    private void close(boolean cleanImage, boolean copyImageToClipboard) {
        if (copyImageToClipboard) {
            AppToolUtil.copyImageToClipboard(parent.getImage());
        }
        if (cleanImage) {
            parent.cleanImage();
        }
        parent.setVisible(false);
        this.dispose();
        this.setVisible(false);
    }

}